package tcm.test.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tcm.test.pojo.dto.OrderDTO;
import tcm.test.pojo.dto.UserDTO;
import tcm.test.pojo.entity.User;
import tcm.test.pojo.entity.result;
import tcm.test.pojo.vo.OrderVO;
import tcm.test.service.UserService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    //注册用户
    @PostMapping
    public result<String> register(@RequestBody UserDTO userDTO) {
        log.info("注册用户：{}", userDTO);
        userService.register(userDTO);
        return result.success();
    }

    //根据id查询用户
    @GetMapping("/{id}")
    public result<User> getById(@PathVariable Long id) {
        log.info("根据id查询用户：{}", id);
        User u = new User();
        u = userService.getById(id);
        return result.success(u);
    }

    //更新指定ID用户的信息
    @PutMapping("/{id}")
    public result<String> update(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        log.info(" 更新指定 ID 用户的信息，{}", userDTO);
        userDTO.setId(id);
        userService.update(userDTO);
        return result.success();
    }

    //删除指定id用户
    @DeleteMapping("/{id}")
    public result<String> delete(@PathVariable Long id) {
        log.info("删除用户{}",id);
        userService.delete(id);
        return result.success();
    }

    //获取指定用户的所有订单列表
    @GetMapping("/{id}/orders")
    public result<List<OrderVO>> getOrders(@PathVariable Long id) {
        log.info("删除用户{}",id);
        List<OrderVO> orderVOS= userService.getOrders(id);
        return result.success(orderVOS);
    }

}
